package com.example.lenovo.musicplayerinit;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private MusicPlayerService.MyBinder musicControl;
    private MyServiceConnect conn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(getApplicationContext(),MusicPlayerService.class);
        conn = new MyServiceConnect();
        bindService(intent,conn,BIND_AUTO_CREATE);

    }

    /**
     * 通过IBinder（connection与onBind）进行service与activity的连接
     * new Binder()  -->  onBind    -->  serviceConnection
     */
    private class MyServiceConnect implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            musicControl = (MusicPlayerService.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
    public void pre(View view) {
        musicControl.callPre();
    }
    public void pause(View view) {
        musicControl.callPause();
    }
    public void play(View view) {
        musicControl.callPlay();
    }
    public void next(View view) {
        musicControl.callNext();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }
}
